#!/usr/bin/env python
#! -*- coding: utf-8 -*-

import datetime

from config import CONF, py_client

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class _DataQuery(object):
    """
    从mongodb查询
    """
    days = datetime.timedelta(days=CONF.DAY_COUNT)
    now_day = datetime.datetime.now().replace(hour=0,minute=0,second=0,microsecond=0,tzinfo=None)
    input_day = now_day - days

    # now_day_1 = now_day - datetime.timedelta(days=CONF.DAY_COUNT-1)
    # print 11111111111,input_day,now_day_1

    db = py_client.miaozhen

    # 处方
    def get_countofprescription(self):
        ret_data = {
            "total":0,
            "auto":0,
            "night_auto":0,
            "need_manual":0
        }
        total = self.db.SFLZ_Prescription.find({
            "prescription_time":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["total"] = total

        auto = self.db.SFLZ_Prescription.find({
            "audit_status":1,
            "prescription_time":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["auto"] = auto

        night_auto = self.db.SFLZ_Prescription.find({
            "audit_status":6,
            "prescription_time":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["night_auto"] = night_auto

        need_manual = self.db.SFLZ_Prescription.find({
            "audit_status":{"$nin":[1,6]},
            "prescription_time":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["need_manual"] = need_manual

        return ret_data

    # 医嘱
    def get_countoforder(self):
        ret_data = {
            "total":0,
            "auto":0,
            "night_auto":0,
            "need_manual":0
        }
        total = self.db.SFLZ_DoctorOrder.find({
            "order_starttime":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["total"] = total

        auto = self.db.SFLZ_DoctorOrder.find({
            "audit_status":1,
            "order_starttime":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["auto"] = auto

        night_auto = self.db.SFLZ_DoctorOrder.find({
            "audit_status":6,
            "order_starttime":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["night_auto"] = night_auto

        need_manual = self.db.SFLZ_DoctorOrder.find({
            "audit_status":{"$nin":[1,6]},
            "order_starttime":{
                "$gte":self.input_day,
                "$lt":self.now_day
            },
            "obsoleted":False
        }).count()
        ret_data["need_manual"] = need_manual

        return ret_data

DataQuery = _DataQuery()
